/*	kd:
	
	The containers for all the wheel items. Spells and weapons are filled in
	automatically by looking for the base classes SharedTome and Weapon,
	while you have to fill in the inventory wheel yourself (since we don't want
	to have base classes for "toggler, but not in the wheel" and "toggler and
	in the wheel" or something stupid like that.
	
	Look for POLNAREFFASSISTMOTHERFUCKER in the event handler nearby. */

// This isn't just pure data because we'll call this from UI context anyway.
class Gcq_SelectableItems play {
	// Base data:
	protected Array <class <Inventory> >	item_classes;
	protected Array <Gcs_ItemType>			handlers;
	protected int							size;

	// Filtered:
	protected Array <int>					found_i;
	protected int 							found_size;

	void Reset () {
		ClearBaseData();
		ClearFiltered();
	}

	void Add (class <Inventory> item, Gcs_ItemType type) {
		if(item && type) {
			item_classes.	push(item);
			handlers.		push(type);
			size++;
		}
	}
	
	int Found (int i) const {
		return found_i [i];
	}
	
	void Remove (int i) {
		if(0 <= size) {
			item_classes.delete(i);
			handlers.delete(i);
			size--;
		}
	}
	
	int, int AmountAndMax (Actor user, int i) const {
		return	handlers [i].Count(		user, item_classes [i]),
				handlers [i].MaxInv(	user, item_classes [i]);
	}
	
	void Use (Actor user, int i) const {
		if(i < size && handlers [i] && item_classes [i]) {
			handlers [i].OnEquip(user, item_classes [i]);
		}
	}
	
	private void FilterAvailable (Actor user) {
		for(int i = 0; i < size; i++)
		if(0 < user.CountInv(item_classes [i])) {
			found_i.push(i);
		}

		found_size = found_i.size();
	}

	// Select those you have in your inventory at least once:
	virtual void FilterByInventory (Actor user) {
		if(!user) {
			return;
		}

		ClearFiltered();
		FilterAvailable(user);
	}

	virtual int InitialSelection (Actor user) const {
		return 0;
	}
	
	int ClassCount () const {
		return size;
	}

	int InventorySize () const {
		return found_size;
	}

	String InventoryName (uint i) const {
		if(i < size) {
			return item_classes [found_i [i]].GetClassName();
		}

		return "";
	}
	
	String RealName (uint i) const {
		if(i < size) {
			return StringTable.Localize(String.Format(
				"$%s_TITLE", item_classes [found_i [i]].GetClassName()) );
		}
		
		return "";
	}
	
	class <Inventory> InventoryType (uint i) const {
		if(i < size) {
			return item_classes [found_i [i]];
		}
		
		return NULL;
	}
	
	Gcs_ItemType HandlerInst (uint i) const {
		if(i < size) {
			return handlers [found_i [i]];
		}
		
		return NULL;
	}
	
	bool Contains (class <Inventory> inv_class) const {
		let i = item_classes.find(inv_class);
		return i < size;
	}

	// Sanity functions:
	private void ClearBaseData () {
		item_classes.	clear();
		handlers.		clear();
		size = 0;
	}

	private void ClearFiltered () {
		found_i.clear();
		found_size = 0;
	}
	
	protected void IgnoreThisTime (int i) {
		if(0 <= found_size) {
			found_i.delete(i);
			found_size--;
		}
	}
}

class Gcq_WeaponSelect : Gcq_SelectableItems {
	void Ignore (class <Weapon> weapon_class) {
		int size	= ClassCount();
		int found	= item_classes.find(weapon_class);
		
		if(0 < size && found != size) {
			Remove(found);
		}
	}
	
	void TsDrawbridgesIsLiterallyStalingrad () {
		// Adds all weapons automatically. I didn't bother making a separate
		// class to keep the list.
		Gcs_ItemType weapon_type = Gcs_ItemTypePack.InstType('Gcs_ItWeapon');
		
		for(int i = allactorclasses.size(); 0 <= --i;)
		if(allactorclasses [i] is 'GuncasterWeapon') {
			Add((class <Inventory>)(allactorclasses [i]), weapon_type);
		}
	}
	
	private void FilterWithAmmo (Actor user) {
		let player = PlayerPawn(user);
		
		if(player)
		for(int i = found_size; 0 <= --i;) {
			let weap = (class <Weapon>)(item_classes [found_i [i]] );
			
			if(!Gc_WeaponHelp.IsSensible(player, weap)) {
				IgnoreThisTime(i);
			}
		}
	}
	
	override void FilterByInventory (Actor user) {
		super.FilterByInventory(user);
		FilterWithAmmo(user);
	}
}

// kd:
// I'm not too sure what kinda items pillow wants in here, so I'll put those
// from the shop that make sense until he wants to add more!

class Gcq_ItemSelect : Gcq_SelectableItems {
	Gcs_ItemType	type_inventory;
	Gcs_ItemType	type_artefact;
	Gcs_ItemType	type_toggler;
	
	void THISISTHEULTIMATE () {
		type_inventory	= Gcs_ItemTypePack.InstType("Gcs_ItInventory");
		type_artefact	= Gcs_ItemTypePack.InstType("Gcs_ItArtefact");
		type_toggler	= Gcs_ItemTypePack.InstType("Gcs_ItToggler");
	}
}

class Gcq_SpellSelect : Gcq_SelectableItems {
	protected Gc_SpellCollection	spells;
	
	// kd:
	// This is actually no longer needed, but I keep it around.
	override int InitialSelection (Actor user) const {
		return spells.SaneUserSpellIndex(user);
	}

	void ChenEatsBreakfast (Gc_SpellCollection spell_list) {
		if(!spell_list) {
			Console.printf("\c[ice]Gcq_SpellSelect::ChenEatsBreakfast: "
				"\c-Passed spell list is null.");
			return;
		}

		Gcs_ItemType spell = Gcs_ItemTypePack.InstType("Gcs_ItSpell");

		if(!spell) {
			Console.printf("\c[ice]Gcq_SpellSelect::ChenEatsBreakfast: "
				"\c-Couldn't initialise the spell item handler! That's REALLY bad!");
			return;
		}

		spells = spell_list;

		// Automatically fill in the selectable spells from whatever is
		// available in the passed list (which will usually contain all spells
		// the mod has to offer).

		int tome_count = spells.TomeCount();
		for(int i = 0; i < tome_count; i++) {
			Add(spells.TomeAt(i), spell);
		}
	}
}

